package game.view.mainUI
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import game.UIComponent.Button;
	import game.common.CustomBitmap;
	import game.common.Reflection;
	import game.controller.GameDispatcher;
	import game.event.EventName;
	import game.event.GameEvent;
	import game.managers.LanguageManager;
	import game.managers.UIManager;
	import game.utils.Utils;
	import game.view.menu.orderMenu.OrderMenu;

	public class MainUI extends Sprite
	{
		/**背景*/
		private var _headBar:CustomBitmap;
		
		/**头像背景*/
		private var _headBG:CustomBitmap;
		
		/**头像*/
		private var _headIcon:CustomBitmap;
		
		/**经验条*/
		private var _exeBarbg:CustomBitmap;
		private var _exeBar:CustomBitmap;
		
		/**经验文字*/
		private var _exetxt:TextField;
		
		private var _nowEx:int=1000;
		
		private var _totalEx:int=1000;
		
		/**玩家名字*/
		//private var _playerName:String="JasonBourne";
		private var _name:TextField;
		
		/**金钱图标*/
		private var _moneyIcon:CustomBitmap;
		private var _money:TextField;
	//	private var _moneytxt:Number=1500;
		
		private var _level:TextField;
		private var _levelIcon:CustomBitmap;
	
		/**金币和经验加成*/
		private var _exeAdd:TextField;
		private var _moneyAdd:TextField;
		private var _addtxt:Number=5;
		
		/**餐厅名字和背景*/
		private var _ctBg:CustomBitmap;
		private var _ctName:TextField;
		//private var _cttxt:String="蛮腰餐厅";
		
		/**右上方按钮*/		
		private var _jiagongIcon:CustomBitmap;	
		private var _fieldIcon:CustomBitmap;
		private var _cowIcon:CustomBitmap;
		private var _selfIcon:CustomBitmap;		
		
		/**左边按钮*/
		private var _helpIcon:CustomBitmap;	
		private var _taskIcon:CustomBitmap;
		private var _giftIcon:CustomBitmap;
		private var _announcementIcon:MovieClip;
		private var _messageIcon:MovieClip;
		private var _newsIcon:CustomBitmap;
		
		/**下边按钮*/
		private var _menuBar:CustomBitmap;
		private var _menuBtn:Button;
		private var _caiBtn:Button;
		private var _friendBtn:Button;
		private var _zxBtn:Button;
		private var _djBtn:Button;
		private var _mouseBtn:Button;
		
		private var _orderMenu:OrderMenu;
		
		private var _dispatcher:GameDispatcher = GameDispatcher.getInstance();
		
		public function MainUI()
		{
			super();
			init();
		}
		
		private function init():void
		{
			initHeadBar();
			initLeftMenu();
			initBottomBar();		
			
			_orderMenu = new OrderMenu();
			_orderMenu.x=710;
			_orderMenu.y=80;
			UIManager.orderMenu = _orderMenu;
			addChild(_orderMenu);
		}
		
		private function initHeadBar():void
		{
//			Global.nowEx=_nowEx;
//			Global.totalEx=_totalEx;
			
			_headBar=new CustomBitmap();
			_headBar.source="headBar";
			_headBar.alpha=0.3;
			addChild(_headBar);
			
			_headBG=new CustomBitmap();
			_headBG.source="headgrid";
			_headBG.x=15;
			_headBG.y=3;
			addChild(_headBG);
			
			_headIcon=new CustomBitmap();
			_headIcon.buttonMode=true;
			_headIcon.source="head";
			_headIcon.name="head";
			_headIcon.x=_headBG.x + 7;
			_headIcon.y=_headBG.y + 7;
			_headIcon.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_headIcon);
			
			_exeBarbg=new CustomBitmap();
			_exeBarbg.source="exeBarbg";
			_exeBarbg.x=_headBG.x + _headBG.width + 3;
			_exeBarbg.y=_headBG.height / 2;
			addChild(_exeBarbg);
			
			_exeBar=new CustomBitmap();
			_exeBar.source="exeBar2";
			_exeBar.x=_exeBarbg.x + 2;
			_exeBar.y=_exeBarbg.y + 2;
			_exeBar.width=(_nowEx / _totalEx) * 120;
			addChild(_exeBar);
			
			_moneyIcon=new CustomBitmap();
			_moneyIcon.source="moneyicon";
			_moneyIcon.x=_exeBar.x;
			_moneyIcon.y=_exeBarbg.y + _exeBarbg.height + 5;
			addChild(_moneyIcon);
			
			_money=Utils.getTextField("1500", 18);
			//Global.money=1500;
			_money.x=_moneyIcon.x + _moneyIcon.width + 10;
			_money.y=_moneyIcon.y - 5;
			addChild(_money);
			
			_exetxt=Utils.getTextField(_nowEx + "/" + _totalEx, 14, 0xffffff);
			_exetxt.x=(_exeBarbg.x + _exeBarbg.width) / 2 + 10;
			_exetxt.y=(_exeBarbg.y + _exeBarbg.height) / 2;
			addChild(_exetxt);
			
			_name=Utils.getTextField(LanguageManager.getWord("message.name"), 18, 0xffffff);
			_name.width=_exeBarbg.width;
			_name.x=_exeBar.x;
			_name.y=_headBG.y;
			addChild(_name);
			
			_levelIcon=new CustomBitmap();
			_levelIcon.source="levelStar";
			_levelIcon.x=210;
			_levelIcon.y=_exeBarbg.y - 15;
			addChild(_levelIcon);
			
			_level=Utils.getTextField("1", 12, 0xffffff);
			_level.x=_levelIcon.x + 10;
			//Global.level=1;
			_level.y=_levelIcon.y + 8;
			addChild(_level);
			
			_moneyAdd=Utils.getTextField("+" + _addtxt + "%", 12);
			_moneyAdd.x=_level.x + 10;
			_moneyAdd.y=_level.y + 30;
			addChild(_moneyAdd);
			
			_exeAdd=Utils.getTextField(_moneyAdd.text, 12);
			_exeAdd.x=_moneyAdd.x + 20;
			_exeAdd.y=_moneyAdd.y - 20;
			addChild(_exeAdd);
			
			_ctBg=new CustomBitmap();
			_ctBg.name="wood"; 
			_ctBg.source="wood";
			_ctBg.x=_exeAdd.x + _exeAdd.width + 70;
			_ctBg.y=5;
			_ctBg.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_ctBg);
			
			_ctName=Utils.getTextField(LanguageManager.getWord("global.name"), 15);
			_ctName.x=_ctBg.x + 40;
			_ctName.y=_ctBg.y + 5;
			addChild(_ctName);
			
			_fieldIcon=new CustomBitmap();
			_fieldIcon.name="field";
			_fieldIcon.source="field";
			_fieldIcon.x=_ctName.x + 300;
			_fieldIcon.y=_ctName.y;
			_fieldIcon.buttonMode=true;
			//_fieldIcon.mouseEnabled=true;
			_fieldIcon.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_fieldIcon);
			
			_cowIcon=new CustomBitmap();
			_cowIcon.name="cow";
			_cowIcon.source="cow";
			_cowIcon.x=_fieldIcon.x + _fieldIcon.width + 10;
			_cowIcon.y=_fieldIcon.y - 3;
			_cowIcon.buttonMode=true;
			//_cowIcon.mouseEnabled=true;
			_cowIcon.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_cowIcon);
			
			_jiagongIcon=new CustomBitmap();
			_jiagongIcon.name="jiagong";
			_jiagongIcon.source="jiagong";
			_jiagongIcon.x=_cowIcon.x+_cowIcon.width + 10;
			_jiagongIcon.y=_cowIcon.y;
			_jiagongIcon.buttonMode=true;
			//_jiagongIcon.mouseEnabled=true;
			_jiagongIcon.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_jiagongIcon);
			
			_selfIcon=new CustomBitmap();
			_selfIcon.name="self";
			_selfIcon.source="self";
			_selfIcon.x=_fieldIcon.x - 65;
			_selfIcon.y=_fieldIcon.y;
			_selfIcon.buttonMode=true;
			//_selfIcon.mouseEnabled=true;
			_selfIcon.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_selfIcon);
		}

		
		private function initLeftMenu():void
		{
			_giftIcon=new CustomBitmap();
			_giftIcon.name="gift";
			_giftIcon.source="giftbox";
			_giftIcon.x=_money.x + 10;
			_giftIcon.y=_money.y + 40;
			//_giftIcon.mouseEnabled=true;
			_giftIcon.buttonMode=true;
			_giftIcon.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_giftIcon);
			
			_taskIcon=new CustomBitmap();
			_taskIcon.name="task";
			_taskIcon.source="task";
			_taskIcon.x=_giftIcon.x - 40;
			_taskIcon.y=_giftIcon.y + 5;
			//_taskIcon.mouseEnabled=true;
			_taskIcon.buttonMode=true;
			_taskIcon.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_taskIcon);
			
			_announcementIcon=Reflection.createMovieClip("gonggao");
			_announcementIcon.name="announce";
			//	_announcementIcon.source="gonggao";
			_announcementIcon.x=_taskIcon.x - 35;
			_announcementIcon.y=_taskIcon.y;
			//_announcementIcon.mouseEnabled=true;
			_announcementIcon.buttonMode=true;
			_announcementIcon.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_announcementIcon);
			
			_helpIcon=new CustomBitmap();
			_helpIcon.source="help";
			_helpIcon.x=_giftIcon.x + _giftIcon.width + 10;
			_helpIcon.y=_giftIcon.y;
			_helpIcon.buttonMode=true;
			//_helpIcon.mouseEnabled=true;
			addChild(_helpIcon);
			
			_messageIcon=Reflection.createMovieClip("liuyan");
			_messageIcon.name="message";
			//		_messageIcon.source="liuyan";
			_messageIcon.x=_helpIcon.x + _helpIcon.width + 10;
			_messageIcon.y=_helpIcon.y;
			//_messageIcon.mouseEnabled=true;
			_messageIcon.buttonMode=true;
			_messageIcon.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_messageIcon);
			
			_newsIcon=new CustomBitmap();
			_newsIcon.name="news";
			_newsIcon.source="news";
			_newsIcon.x=_messageIcon.x + _messageIcon.width + 15;
			_newsIcon.y=_messageIcon.y -3;
			//_newsIcon.mouseEnabled=true;
			_newsIcon.buttonMode=true;
			_newsIcon.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_newsIcon);
		}
		
		
		private function initBottomBar():void
		{
			_menuBar=new CustomBitmap();
			_menuBar.source="menuBar";
			_menuBar.x=210;
			_menuBar.y=560;
			addChild(_menuBar);
			
			_menuBtn=new Button("", "menu");
			_menuBtn.name="menu";
			_menuBtn.x=_menuBar.x + 20;
			_menuBtn.y=_menuBar.y - 30;
			_menuBtn.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_menuBtn);
			
			_caiBtn=new Button("","caishi");
			_caiBtn.name="caishi";
			_caiBtn.x=_menuBtn.x + _menuBtn.width + 30;
			_caiBtn.y=_menuBtn.y;
			_caiBtn.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_caiBtn);
			
			_friendBtn=new Button("","friend");
			_friendBtn.name="friend";
			_friendBtn.x=_caiBtn.x + _caiBtn.width + 30;
			_friendBtn.y=_caiBtn.y;
			_friendBtn.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_friendBtn);
			
			_zxBtn=new Button("","zhuangxiu");
			_zxBtn.name="zhuangxiu";
			_zxBtn.x=_friendBtn.x + _friendBtn.width + 30;
			_zxBtn.y=_friendBtn.y;
			_zxBtn.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_zxBtn);
			
			_djBtn=new Button("","daoju");
			_djBtn.name="daoju";
			_djBtn.x=_zxBtn.x + _zxBtn.width + 30;
			_djBtn.y=_zxBtn.y;
			_djBtn.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_djBtn);
			
			_mouseBtn=new Button("","mouse");
			_mouseBtn.name="mouse";
			_mouseBtn.x=_djBtn.x + _djBtn.width + 30;
			_mouseBtn.y=_djBtn.y;
			_mouseBtn.addEventListener(MouseEvent.CLICK,clickHandler);
			addChild(_mouseBtn);
		}
		
		
		private function clickHandler(e:MouseEvent):void
		{
			switch(e.currentTarget.name)
			{
				case "head":
					_dispatcher.dispatchEvent(new GameEvent(EventName.OPEN_MESSAGE));
					break;
				case "wood":
					_dispatcher.dispatchEvent(new GameEvent(EventName.CHANGE_NAME));
					break;
				case "gift":
					_dispatcher.dispatchEvent(new GameEvent(EventName.OPEN_GIFT));
					break;
				case "task":
					_dispatcher.dispatchEvent(new GameEvent(EventName.OPEN_TASK));
					break;
				case "help":
					_dispatcher.dispatchEvent(new GameEvent(EventName.OPEN_HELP));
					break;
				case "menu":
					_dispatcher.dispatchEvent(new GameEvent(EventName.OPEN_MENU));
					break;
				case "field":
					_dispatcher.dispatchEvent(new GameEvent(EventName.SHOW_FIELD));
					break;
				case "cow":
					_dispatcher.dispatchEvent(new GameEvent(EventName.SHOW_MUCHANG));
					break;
				case "jiagong":
					_dispatcher.dispatchEvent(new GameEvent(EventName.SHOW_JIAGONG));
					break;
				case "self":
					_dispatcher.dispatchEvent(new GameEvent(EventName.SHOW_SELF));
					break;
				case "caishi":
					_dispatcher.dispatchEvent(new GameEvent(EventName.OPEN_LEARN));
					break;
				case "friend":
					_dispatcher.dispatchEvent(new GameEvent(EventName.OPEN_GUYONG));
					break;
				case "zhuangxiu":
					_dispatcher.dispatchEvent(new GameEvent(EventName.OPEN_ZHUANGXIU));
					break;			
				case "daoju":
					_dispatcher.dispatchEvent(new GameEvent(EventName.OPEN_DAOJU));
					break;
				case "mouse":
					_dispatcher.dispatchEvent(new GameEvent(EventName.OPEN_MOUSE));
					break;
				case "message":
					_dispatcher.dispatchEvent(new GameEvent(EventName.OPEN_MESSAGE));
					break;
				case "news":
					_dispatcher.dispatchEvent(new GameEvent(EventName.OPEN_NEWS));
					break;
				case "announce":
					_dispatcher.dispatchEvent(new GameEvent(EventName.OPEN_GONGGAO));
					break;
	
			}
		}
		
		/**
		 * 更新经验条
		 * */
		private function configExeBar():void
		{
			_exetxt.text = _nowEx + "/" + _totalEx;
			
			_exeBar.width=(_nowEx / _totalEx) * 120;	
		}
		
		/**
		 * @private
		 */
		public function set money(value:int):void
		{
			//Global.money = Global.money + value;
			_money.text = value.toString();
		}

		/**当前经验(测试数据)*/
		public function get nowEx():int
		{
			return _nowEx;
		}

		/**
		 * @private
		 */
		public function set nowEx(value:int):void
		{
			if (value >= _totalEx) 
			{
				_totalEx =  _totalEx + 200;
				Global.totalEx = _totalEx;
				_level.text = String(int(_level.text) + 1);
				Global.level = int(_level.text);
				_nowEx = 0;
				Global.nowEx = _nowEx;
			}
			else{
				_nowEx = value;
				Global.nowEx = _nowEx;				
			}
			
			configExeBar();
		}

		/**金钱文字*/
		public function get money():int
		{
			return int(_money.text);
		}

		/**总经验(测试数据)*/
		public function get totalEx():int
		{
			return _totalEx;
		}

		/**
		 * @private
		 */
		public function set totalEx(value:int):void
		{
			_totalEx = value;
			
			configExeBar();
		}

		/**等级图标和文字*/
		public function get levelTxt():String
		{
			return _level.text;
		}

		/**
		 * @private
		 */
		public function set level(value:int):void
		{
			_level.text = value.toString();
		}


	}
}